package quq.missq.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;

import java.util.ArrayList;

import quq.missq.BaseActivity;
import quq.missq.Constants;
import quq.missq.MLog;
import quq.missq.R;
import quq.missq.beans.MyInformationBean;
import quq.missq.huanxin.Constant;
import quq.missq.utils.BitmapUtils;
import quq.missq.utils.ImageLoadInterface;
import quq.missq.utils.ImageLoadUtil;
import quq.missq.utils.VolleyTool;

/**
 * Created by wcx on 2015/9/21.
 */
public class OtherInfoActivity extends BaseActivity {
    private ImageView mAvatar;
    private TextView mUserName;
    private TextView mUserType;
    private TextView mLevel;
    private ImageView mGender;

    private TextView accountName;
    private TextView accountTall;
    private TextView accountBrith;
    private TextView accountStar;
    private TextView accountSchool;
    private TextView accountGoschool;
    private LinearLayout llHeadBg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otherinfo);
        initView();
        initData();
    }

    private void initData() {
        MyInformationBean info = (MyInformationBean) getIntent().getSerializableExtra("OtherInfo_1");
        final MyInformationBean.Data data = info.getData();
        String bgUrl = info.getData().getBackground();
        if (bgUrl != null && !bgUrl.isEmpty()) {
            ImageLoadUtil.loadBitmapImage(Constants.IMAGE_HOST + bgUrl, new ImageLoadInterface.ImageLoadCallBack() {
                @Override
                public void callBack(Bitmap bitmap) {
                    llHeadBg.setBackgroundDrawable(new BitmapDrawable(bitmap));
                }
            });
//            VolleyTool.getImageLoader().get(Constants.IMAGE_HOST + bgUrl, new ImageLoader.ImageListener() {
//                @Override
//                public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
//                    if (imageContainer != null && imageContainer.getBitmap() != null) {
//                        llHeadBg.setBackgroundDrawable(new BitmapDrawable(getResources(), imageContainer.getBitmap()));
//                    }
//                }
//
//                @Override
//                public void onErrorResponse(VolleyError volleyError) {
//
//                }
//            });
        }
        VolleyTool.getImageLoader().get(Constants.IMAGE_HOST + data.getAvatar80(), new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
                if (imageContainer.getBitmap() != null) {
                    mAvatar.setImageBitmap(BitmapUtils.toRoundBitmap(imageContainer.getBitmap(), 3, R.color.white));
                }
            }

            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        });
        mAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(OtherInfoActivity.this,ImageViewPagerActivity.class);
                ArrayList<String> urls = new ArrayList<>();
//                http://app.missquq.com//store/ava/247/80/0/0.jpg?t=1444570185000
                int chart = data.getAvatar().indexOf('?');
                urls.add(Constants.IMAGE_HOST + data.getAvatar().substring(0,chart));
                intent.putExtra("netImageUrls",urls );
//                intent.putExtra("netImageUrls",Constants.IMAGE_HOST + data.getAvatar());
//                MLog.e(Constants.IMAGE_HOST + data.getAvatar());
                startActivity(intent);
            }
        });
        mUserName.setText(getDefaultValue(data.getNkname()));
        mGender.setImageResource(getGender(data.getGender()));
        mUserType.setText(getDefaultValue(getUserType(data.getAuth())));
        mLevel.setText(getDefaultValue("level" + data.getLevel()));

        accountName.setText(getDefaultValue(data.getName()));
        accountTall.setText(getDefaultValue(data.getHeight() + ""));
        accountBrith.setText(getDefaultValue(data.getBirthday()));
        accountStar.setText(getDefaultValue(data.getAstro()));
        accountSchool.setText(getDefaultValue(data.getSchoolName()));
        accountGoschool.setText(getDefaultValue(data.getEnroll() + ""));
    }


    private String getDefaultValue(String defaultValue) {
        if ("".equals(defaultValue) || defaultValue == null) {
            return "无内容";
        }
        return defaultValue;
    }


    private void initView() {
        llHeadBg  = (LinearLayout) findViewById(R.id.avtarLayout);
        mAvatar = (ImageView) findViewById(R.id.avatar);//头像

        mUserName = (TextView) findViewById(R.id.userName);//名字
        mGender = (ImageView) findViewById(R.id.gender);//性别
        mUserType = (TextView) findViewById(R.id.userType);//用户类型
        mLevel = (TextView) findViewById(R.id.level);//用户级别

        accountName = (TextView) findViewById(R.id.tv_account_name);
        accountTall = (TextView) findViewById(R.id.tv_account_tall);
        accountBrith = (TextView) findViewById(R.id.tv_account_brith);
        accountStar = (TextView) findViewById(R.id.tv_account_star);
        accountSchool = (TextView) findViewById(R.id.tv_account_school);
        accountGoschool = (TextView) findViewById(R.id.tv_account_goschool);
    }

    private int imageIds[] = {R.mipmap.boy_icon, R.mipmap.girl_icon};
    private String userType[] = {"普通用户", "加V用户", "女神用户"};

    public int getGender(int gender) {
        if (gender == 1) {
            return imageIds[0];
        } else if (gender == 2) {
            return imageIds[1];
        }
        return 0;
    }

    public String getUserType(int auth) {
        return userType[auth];
    }
}
